import { getBaseUrl } from '@/services/urlconst';
import { getFileNameByPath, getKeyRouter, requestFunComponFiex } from '@/utils';
import { useState } from 'react';
import { useRequest } from 'umi';

const baseUrl = getBaseUrl();

/**
 * 转化ofd文件
 * @param callback 接口回调
 * @returns
 */
const convertOfdSeavice = (callback: (res: any, clickType: boolean) => void) => {
  const convertOfd = useRequest(
    (fieldPath: any) => {
      const fieldName: string = getFileNameByPath(fieldPath);
      const fileUrl: string = encodeURI(fieldPath);
      let bodyParams = {
        BASE_DATA: {
          SYS_ID: 1,
          SERIAL_NUMBER: `ofd-${getKeyRouter()}${getKeyRouter()}`,
        },
        META_DATA: {
          IS_ASYN: false,
          IS_MERGER: true,
          IS_IMAGE: false,
        },
        FILE_LIST: [
          {
            FILE_NO: fieldName?.split('.')?.[0],
            FILE_ATTRIBUTE: 0,
            FILE_INDEX: 1,
            FILE_TYPE: fieldName?.split('.')?.[1],
            CONVERT_TYPE: 'ofd',
            RESPONSE_TYPE: 1,
            FILE_PATH: fileUrl,
          },
        ],
      };
      // let reqUrl = `${getFieldConvertUrl()}/DCS/convert/common`;
      let reqUrl = `${baseUrl}/DCS/convert/common`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, true);
      },
    },
  );
  return {
    convertOfdRun: convertOfd?.run,
    convertOfdLoading: convertOfd?.loading,
  };
};

/**
 * 设置打印份数
 * @param callback
 * @returns
 */
const settingPrintNumService = (callback: (res: Record<string, any>) => void) => {
  const settingDocPrintNum = useRequest(
    (payload: any) => {
      let bodyParams = {
        isAppend: false,
        docId: `R${payload?.contractCode}`,
        type: 2,
        limit: payload?.contractNum,
      };
      // let reqUrl = `${getPrintUrlDJ()}/ESS/api/settingDocPrintNumByName`;
      let reqUrl = `${baseUrl}/ESS/api/settingDocPrintNumByName`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );

  return {
    settingDocPrintNumRun: settingDocPrintNum?.run,
    settingDocPrintNumLoading: settingDocPrintNum?.loading,
  };
};

/**
 * 获取剩余打印份数
 * @param callback
 * @returns
 */
const getDocPrintNumService = (callback: (res: Record<string, any>, rowInfo: Record<string, any>) => void) => {
  const [savePayload, setSavePayload] = useState<Record<string, any>>({});

  const getDocPrintNumService = useRequest(
    (payload: any) => {
      let bodyParams = {
        docId: `R${payload?.contractCode}`,
        type: 2,
      };

      setSavePayload(payload);
      // let reqUrl = `${getPrintUrlDJ()}/ESS/api/getDocPrintNumByType`;
      let reqUrl = `${baseUrl}/ESS/api/getDocPrintNumByType`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, savePayload);
      },
    },
  );

  return {
    getDocPrintNumServiceRun: getDocPrintNumService?.run,
    getDocPrintNumServiceLoading: getDocPrintNumService?.loading,
  };
};

export { convertOfdSeavice, settingPrintNumService, getDocPrintNumService };
